// app.js
App({
	data: {
        imgurl: "https://www.jiyuanneng.com/wximg/",
        rooturl: "https://www.jiyuanneng.com/api/miaozhimini/",
	},
	globalData: {
        userId:0,
        userinfo:[],
        userPhone:'',
        userFid:0
	},
	/**
     * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
     */
    onLaunch: function (options) {
        var userFid = options.userFid;
        if(userFid == 0 || userFid == '' || userFid == undefined){
            this.globalData.userFid = 0;
        }else{
            this.globalData.userFid = userFid;
            wx.setStorageSync('userFid',userFid);
        }
        //wx.setStorageSync('token','78c0cd5a5cfc8030bf7e2410b3a8443a');
        //this.getUserInfo();
        this.wxLogin();
	},
    wxLogin: function() {
        var that=this
        wx.showLoading({
            title: '加载中',
        })
        wx.login({
            success(res) {
                if (res.code) {
                    //发起网络请求
                    wx.request({
                        url: that.data.rooturl + 'weixin/getOpenId',
                        data: {  js_code: res.code},
                        success: function (res) {
                            wx.hideLoading();
                            var data = res.data.data;
                            if (data.code < 0) {
                                wx.showToast({
                                    title: "网络异常",
                                    icon: 'loading'
                                })
                                return;
                            }
                            wx.setStorageSync('token', data);//获取到token
                            that.getUserInfo();
                        }
                    })
                } else {
                    console.log('登录失败！' + res.errMsg);
                    wx.showToast({
                        title: "网络异常",
                        icon: 'loading'
                    })
                }
            },
            fail(res) {
                console.log(res);
                wx.showToast({
                    title: "网络异常",
                    icon: 'loading'
                })
            }
        })
    },
    //用户登录
    getUserInfo:function(){
        var that=this;
        wx.request({
            url: that.data.rooturl + 'weixin/getUserInfo',
            data: {},
            header:{token:wx.getStorageSync('token')},
            success: function (res) {
                wx.hideLoading();
                var data = res.data.data;
                console.log(data);
                if(data.loginStatus == 2){
                    that.globalData.userId = data.id;
                    that.globalData.userinfo = data;
                    that.globalData.userPhone = data.userPhone;
                }
            }
        })
    },
	//获取当前页面全路径
    getCurPage: function(){
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面的对象
        var url = currentPage.route //当前页面url
        var options = currentPage.options //如果要获取url中所带的参数可以查看options
        var params = ''
        for(var i in options){
            params += '-' + i + '|' + options[i]
        }
        if (params) {
            params = params.substring(1)
        }
        return [url, params]
    },
    makeFullPage: function(url) {
        var prev_url = ''
        url = url.split(',')
        if(url[1]==''||url[1]==null||url[1]==undefined){
            return url[0]
        }else{
            var params = url[1].split('-')
        
            for (var i in params) {
                prev_url += '&' + params[i].split('|').join('=')
            }
            return url[0] + '?' + prev_url.substring(1)
        }
	},
    setShareMessage: function(){
        var that=this
        var title = '极元能';
        var imageUrl = '';
        var url = "/pages/index/index?userFid="+  that.globalData.userId;
        return {
            title: title,
            imageUrl: imageUrl,
            path: url
        }
    }
})
